package algorithm.array;

public class MyOrderArray {

  public static void main(String[] args) {
    int[] array = {9232, 12, 3214, 534, 22, 4423, 13, 445, 64567, 787, 853, 1};
    long s = System.currentTimeMillis();
    System.out.println(search(array, 1) + ", use time: " + (System.currentTimeMillis() - s));
    System.out.println(binarySearch(array, 1) + ", use time: " + (System.currentTimeMillis() - s));
  }

  /**
   * ��ѯ
   * 
   * @param array
   * @return ����λ�ã�-1��ʾδ�ҵ�
   */
  public static int search(int[] array, int value) {
    for (int i = 0; i < array.length; i++) {
      if (array[i] == value) {
        return i;
      }
    }
    return -1;
  }

  /**
   * ���ֲ��ң����������Ǵ�С����˳������
   */
  public static int binarySearch(int[] array, int value) {
    int middlePos = 0;
    int lowPos = 0;
    int maxPos = array.length;
    while (true) {
      middlePos = (lowPos + maxPos) / 2;
      if (array[middlePos] == value) {
        return middlePos;
      } else if (lowPos > maxPos) {
        return -1;
      } else {
        if (array[middlePos] > value) {
          maxPos = middlePos - 1;
        } else {
          lowPos = middlePos + 1;
        }
      }
    }
  }

  /**
   * ð������
   * 
   * @param array δ���������
   * @return ��������ɵ�����
   */
  public static int[] bubbleSort(int[] array) {
    int[] sortedArr = new int[array.length];
    int temp = 0;
    for (int i = 0; i < array.length; i++) {

    }
    return sortedArr;
  }

}
